<?php
namespace Home\Controller;
use Think\Controller;
class YygameController extends Controller {
    public function index(){
        // $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');

      //读取验证参与码
      if(IS_POST)
      {
         $code=I("post.code");

         $where['code']=trim($code);
         $where['status']=0;
         $code_id=M("yycode")->where($where)->getField("code_id");
         if($code_id)
         {

           //标记随机码已使用
           $update_data['used_time']=time();
           $update_data['status']=1;
           $status= M("yycode")->where("code_id='$code_id' and status=0")->save($update_data);
           if($code_id)
           {
            $_SESSION['code_id']=$code_id;
            $_SESSION['code']=$code;//判断抽奖是否结束
           }
           

           $this->redirect("Yygame/shake_index");
           exit();
            
         }
         else
         {
           $err_where['code']=$code;
           $status=M("yycode")->where($err_where)->getField("status");
           if($status)
           {
                
                $this->show("<script>alert('参与码已使用')</script>");        
           }
           else
           {
             $this->show("<script>alert('参与码有误')</script>");
           }

           $this->assign("code",$code);

         }
      }
    


          

      $this->display();



    }
    
    //摇奖首页
    public function shake_index()
    {
      if(empty($_SESSION['code']))
      {
        $this->redirect("Yygame/index");exit();
      }
      

      $this->display();

    }



    //摇奖结果
    public function shake_result()
    {
      //摇奖
      if($_SESSION['code'])
      {
        $prozen=D("Yygame")->chose_prizes();
      }


      $this->display();

    }
    //提交获奖用户
    public function add_users()
    {

      //未提交信息
      if($_SESSION['prizes_id'])
      {  
        $prizes_id=$_SESSION['prizes_id'];
        $prizes_name=M("yyprizes")->where("prizes_id='$prizes_id'")->getField("prizes_name");
        
      }
      //提交信息ID
      $yylog_id=$_SESSION['yylog_id'];
      //提交信息
    	if(IS_POST&&$_SESSION['prizes_id'])
    	{
        $post=I();
        $data['name']=$post['name'];
        $data['phone']=$post['phone'];
        $data['address']=$post['address'];
        $data['wechat']=$post['wechat'];
        $data['code_id']=$_SESSION['code_id'];
        $data['prizes_id']=$_SESSION['prizes_id'];
        $data['prizes_name']=$prizes_name;
        $data['ctime']=time();
      



         $yylog_id=D("Yygame")->add_users($data);

    	}     


      //已提交信息
      if($yylog_id&&empty($_SESSION['prizes_id']))
      {
          
          $info=M("yyprizes_log")->where("yylog_id='$yylog_id'")->find();
          $prizes_name=$info['prizes_name'];

          $this->assign("info",$info);
      }

      $this->assign("prizes_name",$prizes_name);

    //特殊跳转
      if(empty($yylog_id)&&empty($_SESSION['prizes_id'])){$this->redirect("Yygame/index");exit();}

      $this->display();
    }


}